<template>
  <div>
    <header class="custom-header">
      <div class="logo">
        <img src="@/assets/icon/Logo.png" alt="Logo" class="icon" />
        <span class="site-name">红星党建</span>
      </div>

      <nav class="custom-nav">
        <router-link
          v-for="item in menuItems"
          :key="item.index"
          :to="item.path"
          class="nav-item"
          :class="{ active: activeRoute === item.path }"
          @mouseenter="item.hover = true"
          @mouseleave="item.hover = false"
        >
          {{ item.name }}
        </router-link>
      </nav>

      <div class="avatar" @click="toggleDropdown">
        <img src="@/assets/icon/Logo.png" alt="头像" class="avatar-icon" />
        <div v-if="isDropdownVisible" class="dropdown">
          <router-link to="/personal-center">个人中心</router-link>
          <a @click.prevent="logout">退出登录</a>
        </div>
      </div>
    </header>

    <main>
      <router-view />
    </main>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  name: "Layout",
  setup() {
    const isDropdownVisible = ref(false);
    const activeRoute = ref(window.location.pathname); // 获取当前URL

    const toggleDropdown = () => {
      isDropdownVisible.value = !isDropdownVisible.value;
    };

    const logout = () => {
      alert("退出登录");
      // 实际的退出逻辑，例如清除 token
    };

    const menuItems = [
      { name: '首页', path: '/user/home' },
      { name: '党建地点', path: '/user/location' },
      { name: '论坛', path: '/user/forum' },
      { name: '公告资讯', path: '/user/news' },
      { name: '商城', path: '/user/store' },
      { name: '个人中心', path: '/user/profile' }
    ];

    return {
      isDropdownVisible,
      toggleDropdown,
      logout,
      menuItems,
      activeRoute,
    };
  },
};
</script>

<style scoped>
.custom-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #ffffff; /* 背景色为白色 */
  padding: 10px 20px;
  color: black;
}

.logo {
  display: flex;
  align-items: center;
}

.icon {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.site-name {
  font-size: 24px;
  font-weight: bold;
}

.custom-nav {
  display: flex;
  justify-content: center;
  flex: 1;
}

.nav-item {
  margin: 0 15px;
  text-decoration: none; /* 去掉默认下划线 */
  font-size: 20px; /* 字体大小 */
  color: black; /* 初始字体颜色 */
  transition: color 0.3s, background-color 0.3s; /* 动画过渡 */
  position: relative; /* 用于下面的横线 */
}

.nav-item:hover {
  color: yellow; /* 悬停时颜色 */
  background-color: rgba(255, 255, 0, 0.1); /* 悬停时背景颜色变浅 */
}

.nav-item.active {
  color: yellow; /* 选中时颜色 */
}

/* 选中状态下的横线 */
.nav-item.active::after {
  content: '';
  display: block;
  height: 3px; /* 横线高度 */
  background-color: yellow; /* 横线的颜色 */
  position: absolute;
  left: 0;
  right: 0;
  bottom: -5px; /* 横线距离文本的距离 */
}

.avatar {
  position: relative;
  cursor: pointer;
}

.avatar-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.dropdown {
  position: absolute;
  right: 0;
  background-color: white;
  color: black;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.dropdown a {
  display: block;
  padding: 10px;
  text-decoration: none;
  color: black;
}

.dropdown a:hover {
  background-color: #f0f0f0;
}
</style>
